//
//  EditingViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/13.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "EditingViewController.h"
#import "XMTextView.h"

@interface EditingViewController ()<UITextViewDelegate>
@property(nonatomic, strong)UIButton *rightButton;
@property(nonatomic, strong)UIView *contentView;
@property(nonatomic, strong)UITextField *nameTextField;
@property(nonatomic, strong)UserAccount *account;
@property(nonatomic, strong)XMTextView *textView;
@end

@implementation EditingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = self.titleText;

    self.account = [UserAccountTool account];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor xm_somberColor] forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor xm_MainColor] forState:UIControlStateSelected];
    self.rightButton.enabled = NO;
    [self.rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.width, 60)];
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contentView];
    
    if ([self.titleText isEqualToString:@"昵称"]) {
        [self.contentView addSubview:self.nameTextField];
    }else if ([self.titleText isEqualToString:@"个人简介"]){
        self.contentView.height = 110;
        [self.contentView addSubview:self.textView];
    }

}

-(void)nameTextFieldTextChange:(UITextField *)textField{
    self.rightButton.selected = textField.text.length > 0;
}
- (void)textViewDidChange:(UITextView *)textView{
    self.rightButton.selected = textView.text.length > 0;
}
-(void)rightButtonClick:(UIButton *)sender{
    NSDictionary * dict = @{@"NikeName":self.nameTextField.text};
    [FBNetworkTool get:@"userapi/com.UpdateUserInfo" parameters:dict showHUD:YES success:^(XMHttpResponseModel *responseModel) {
        NSLog(@"responseModel == %@",responseModel);
    } failure:^(NSError *error) {
        
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 40)];
        [_nameTextField addTarget:self action:@selector(nameTextFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
        if (self.account.NikeName && self.account.NikeName.length > 0) {
            _nameTextField.placeholder = self.account.NikeName;
        }else{
            _nameTextField.placeholder = @"请输入昵称";
        }
    }
    return _nameTextField;
}

-(XMTextView *)textView{
    if (!_textView) {
        _textView = [[XMTextView alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 20, 100)];
        _textView.placeholder = @" 介绍一下你自己吧";
        _textView.font = [UIFont xm_font:17.0];
        _textView.delegate = self;
        _textView.maxNumber = 30;
    }
    return _textView;
}

@end
